﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

// 版权所有：2014  All Rights Reserved.
// 文 件 名：树型控件
// 功能描述：
// 创建标识：m.sh.lin0328@163.com 2014/12/13 22:27:05
// 修改描述：
//----------------------------------------------------------------*/
namespace MslDiary.Utils
{
    public class TreeHelper
    {
        /// <summary>
        /// 绑定树
        /// </summary>
        public static List<TreeNodeViewModel> Bind(List<TreeNodeViewModel> nodeList)
        {
            List<TreeNodeViewModel> treeNodeList = new List<TreeNodeViewModel>();
            TreeNodeViewModel node = null;
            for (int i = 0; i < nodeList.Count; i++)
            {
                if (nodeList[i].ParentID == -1 || nodeList[i].ParentID == 0)//根
                {
                    treeNodeList.Add(nodeList[i]);
                }
                else
                {
                    node = FindNodeById(nodeList, nodeList[i].ParentID);
                    if (node == null)
                        continue;
                    nodeList[i].ParentNode = node;
                    node.TreeNodeList.Add(nodeList[i]);
                }
            }
            return treeNodeList;
        }
        /// <summary>
        /// 递归向下查找
        /// </summary>
        public static TreeNodeViewModel FindNodeById(List<TreeNodeViewModel> nodeList, int id)
        {
            if (nodeList == null) return null;
            for (int i = 0; i < nodeList.Count; i++)
            {
                if (nodeList[i].ID == id)
                {
                    return nodeList[i];
                }
                TreeNodeViewModel node = FindNodeById(nodeList[i].TreeNodeList, id);

                if (node != null)
                {
                    return node;
                }
            }
            return null;
        }
        /// <summary>
        /// 从 List<TreeNodeViewModel> 中查找 IsExpanded = True 的 Id 集合
        /// </summary>
        /// <param name="treeNodeList"></param>
        /// <returns></returns>
        public static ArrayList GetExpandedId(List<TreeNodeViewModel> treeNodeList)
        {
            ArrayList al = new ArrayList();
            if (treeNodeList == null || treeNodeList.Count == 0) return al;
            //if (treeNodeList.IsExpanded)
            //    al.Add(treeNode.ID);
            ArrayList alTmp = new ArrayList();
            foreach (TreeNodeViewModel treeNode in treeNodeList)
            {
                alTmp = GetSubExpandedId(treeNode);
                if (alTmp.Count > 0)
                    al.AddRange(alTmp);
            }
            return al;
        }
        private static ArrayList GetSubExpandedId(TreeNodeViewModel treeNode)
        {
            ArrayList al = new ArrayList();
            if (treeNode == null) return al;
            if (treeNode.IsExpanded)
                al.Add(treeNode.ID);

            if (treeNode.TreeNodeList == null || treeNode.TreeNodeList.Count == 0)
                return al;
            ArrayList alTmp = new ArrayList();
            foreach (TreeNodeViewModel item in treeNode.TreeNodeList)
            {
                alTmp = GetSubExpandedId(item);
                if (alTmp.Count > 0)
                    al.AddRange(alTmp);
            }

            return al;
        }
    }
    /// <summary>
    /// 树控件节点
    /// </summary>
    public class TreeNodeViewModel : MslDiary.Framework.ViewModelBase
    {
        bool isExpanded;
        bool isSelected;
        int num;
        TreeNodeViewModel selectedNode;
        public TreeNodeViewModel()
        {
            this.TreeNodeList = new List<TreeNodeViewModel>();
            this.ParentID = -1;
            //this.IsExpanded = true;
        }
        /// <summary>
        /// 被选中的节点
        /// </summary>
        public TreeNodeViewModel SelectedNode
        {
            get { return selectedNode; }
            set
            {
                //base.SetProperty<TreeNodeViewModel>(ref this.selectedNode, value);
                if (this.selectedNode == value)
                    return;
                this.selectedNode = value;
                this.RaisePropertyChanged("selectedNode");
            }
        }
        public TreeNodeViewModel ParentNode { get; set; }
        public int ID { get; set; }
        public string DisplayName { get; set; }
        public string Icon { get; set; }
        public int Num
        {
            get
            {
                return num;
            }
            set
            {
                //base.SetProperty<int>(ref this.num, value);
                if (this.num == value)
                    return;
                this.num = value;
                this.RaisePropertyChanged("Num");
            }
        }
        public int ParentID { get; set; }
        public bool IsExpanded
        {
            get
            {
                return isExpanded;
            }
            set
            {
                //base.SetProperty<bool>(ref this.isExpanded, value);
                if (this.isExpanded == value)
                    return;
                this.isExpanded = value;
                this.RaisePropertyChanged("IsExpanded");
            }
        }
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                //base.SetProperty<bool>(ref this.isSelected, value);
                if (this.isSelected == value)
                    return;
                this.isSelected = value;
                this.RaisePropertyChanged("IsSelected");
            }
        }
        public List<TreeNodeViewModel> TreeNodeList { get; set; }
    }
}
